package cn.cs.yangtai.controller;

import cn.cs.yangtai.entity.Commodity;
import cn.cs.yangtai.entity.vo.CommodityVo;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.entity.vo.ResponsePojo;
import cn.cs.yangtai.service.CommodityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 商品表(Commodity)表控制层
 *
 * @author makejava
 * @since 2024-12-17 13:50:54
 */
@RestController
@CrossOrigin
@Tag(name = "商品")
@RequestMapping("commodity")
public class CommodityController {
    /**
     * 服务对象
     */
    @Resource
    private CommodityService commodityService;

    /**
     * 查询销量排行前10的商品
     *
     * @return 商品列表
     */
    @Operation(summary = "精选商品")
    @GetMapping("top10sales")
    public ResponseEntity<ResponsePojo<List<CommodityVo>>> findTop10BySales() {
        List<CommodityVo> top10Commodities = commodityService.findTop10BySales();
        return ResponseEntity.ok(ResponsePojo.success(top10Commodities));
    }

    /**
     * 查询距离当前时间最近的商品
     * @return 商品列表
     */
    @Operation(summary = "最新商品")
    @GetMapping("recent")
    public ResponseEntity<ResponsePojo<List<CommodityVo>>> findRecentCommodities() {
        List<CommodityVo> recentCommodities = commodityService.findRecentCommodities();
        return ResponseEntity.ok(ResponsePojo.success(recentCommodities));
    }

    /**
     * 分页查询
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @Operation(summary = "分页展示商品")
    @GetMapping
    public ResponsePojo<PageResult<CommodityVo>> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageResult<CommodityVo> result=commodityService.queryByPage(cuttentPage,pageSize);
        return ResponsePojo.success(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过id找商品")
    @GetMapping("{id}")
    public ResponseEntity<CommodityVo> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.commodityService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @Operation(summary = "添加商品")
    @PostMapping
    public ResponseEntity<String> add(String cName,Integer cFid,String cSpeci,String cPd,Double cPrice,Integer cQuantity,Integer cSales) {
        Commodity commodity=new Commodity();
        commodity.setCName(cName);
        commodity.setCFid(cFid);
        commodity.setCSpeci(cSpeci);
        commodity.setCPd(cPd);
        commodity.setCPrice(cPrice);
        commodity.setCQuantity(cQuantity);
        commodity.setCSales(cSales);
        commodity.setCDate(new Date());
        commodityService.insert(commodity);
        return ResponseEntity.ok().body("商品添加成功");
    }

    /**
     * 编辑数据
     *
     * @return 编辑结果
     */
    @Operation(summary = "修改商品")
    @PutMapping
    public ResponseEntity<Commodity> edit(Integer cId,String cName,Integer cFid,String cSpeci,String cPd,Double cPrice,Integer cQuantity,Integer cSales) {
        Commodity commodity=new Commodity();
        commodity.setCId(cId);
        commodity.setCName(cName);
        commodity.setCFid(cFid);
        commodity.setCSpeci(cSpeci);
        commodity.setCPd(cPd);
        commodity.setCPrice(cPrice);
        commodity.setCQuantity(cQuantity);
        commodity.setCSales(cSales);
        commodity.setCDate(new Date());
        return ResponseEntity.ok(this.commodityService.update(commodity));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @Operation(summary = "通过id删除商品")
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.commodityService.deleteById(id));
    }

}

